<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>	
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path;
%>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>会员登录</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="<%=basePath %>/css/open/mp/common.css?v=2018030502" type="text/css">
<link rel="stylesheet" href="<%=basePath %>/css/open/mp/userinfo.css" type="text/css">
<link rel="stylesheet" href="<%=basePath %>/css/open/mp/login.css?v=2018031601" type="text/css">
</head>
<body>
<div id="container">
    <div class="head">
        <img id="logo2" src="" onerror="javascript:this.src='<%=basePath %>/images/open/mp/logo_default.png'" style="z-index: 2;"/>
        <img id="bg_thumbnail" class="bg_thumbnail" src="" style="position: absolute; width: 100%; height: 100%; z-index: 1;top: 0px;">
    </div>
    <div class="form">
        <div>
            <span>手机号</span>
            <input id="mobile" class="txt" type="number" name="mobile" maxlength="11" placeholder="请填写会员手机号" value="${mobile }"/>
            <input type="button" id="sendCodeBtn" value="发送验证码"/>
            <!-- <span style="color: #999; font-size: 12px; position: absolute; top: 194px; right: 65px">25s后发送</span> -->
        </div>
        <div>
            <span>验证码</span>
            <input id="qrcode" type="number" class="txt" name="code" placeholder="请输入验证码" />
        </div>
        <input type="button" value="登 录" id="loginBtn"></input>
    </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
var path = "<%=path%>";
var icon = "${icon}";
var nick_name = "${nick_name}";
var appId = "${appId}";
var noncestr = "${noncestr}";
var timestamp = "${timestamp}";
var signature = "${signature}";
wx.config({
    debug: false,
    appId: appId,
    timestamp: timestamp,
    nonceStr: noncestr,
    signature: signature,
    jsApiList: ["checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage"]
});
wx.ready(function () {
	wx.onMenuShareTimeline({
	    title: '会员中心',
	    link: window.location.href,
	    imgUrl: icon,
	    success: function () {
		},
		cancel: function () {
	    }
	});
	wx.onMenuShareAppMessage({
		title: '会员中心',
		desc: nick_name,
		link: window.location.href,
		imgUrl: icon,
		type: '', // 分享类型,music、video或link，不填默认为link
		dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
		success: function () {
		},
		cancel: function () {
		}
	});
})
function GetQueryString(name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
function reloadInfo(){
	var baseInfoUrl = path+'/open/me/baseInfo?type=single';
	/* alert(baseInfoUrl) */
	$.ajax({
		url:baseInfoUrl,
		type:"GET",
		timeout: 10000,
		success:function (data) {
			var result = data;
			/* alert(0000+":"+JSON.stringify(result)); */
			if(result.status == 200){
				if(typeof(result.background) != 'undefined' && result.background != ''){
					console.log('result.background:'+result.background)
					$('#bg_thumbnail').attr('src',result.background)
				}
				if(typeof(result.logo) != 'undefined' && result.logo != ''){	
					console.log('result.logo:'+result.logo)
					$('#logo2').attr('src',result.logo)
				}
			}else{
				alert('返回内容发生错误！')
				/* alert(1111+":"+JSON.stringify(result)); */
			}
		},
		error:function(data){
			alert('服务器错误！')
			/* alert(2222+":"+JSON.stringify(result)); */
		}
	})
}
function init(){
	$.removeCookie('mobile', { path: '/' });
	$.removeCookie('isCRMer', { path: '/' });
	$.removeCookie('uid', { path: '/' });
	$.removeCookie('user_name', { path: '/' });
	$.removeCookie('account', { path: '/' });
	$.removeCookie('logid', { path: '/' });
	$.removeCookie('access_token', { path: '/' });
	reloadInfo();
}
$(function(){
	init();
	var ua = navigator.userAgent.toLowerCase(); 
	var errormsg = '${errormsg}'
	if(errormsg != ''){
		alert(errormsg);
	}
	var wx_user_name = '${wx_user_name}'
	var component_appid = '${wx_component_appid}'
	
	var tenant_id = GetQueryString('cid');
	if(tenant_id == null || tenant_id == ''){
		tenant_id = $.cookie('cid');
	}else{
		$.cookie('cid',tenant_id,{path:"/"});
	}
	//微信端检测是否有openid
	if(ua.indexOf("micromessenger") > -1){
		/* alert('appid='+wx_user_name+',component_appid='+component_appid) */
		var openid = $.cookie('openid');
		if((typeof(openid) == "undefined" || openid == '') && ((typeof(wx_user_name) != "undefined" || wx_user_name != ''))){
			var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+wx_user_name+'&redirect_uri='+encodeURIComponent('http://'+document.domain+'/open/me?cid='+tenant_id)+'&response_type=code&scope=snsapi_base&state=1234&component_appid='+component_appid+'#wechat_redirect';
			//https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE&component_appid=component_appid#wechat_redirect
			//alert(url);
			window.location = url;
		}
	}

	var countdown=60; 
	function settime(val) { 
		if (countdown == 0) { 
			val.removeAttribute("disabled"); 
			val.value="发送验证码"; 
			val.style.color = '#1bb495';
			countdown = 60; 
		} else { 
			val.setAttribute("disabled", true); 
			val.value="重新发送(" + countdown + ")"; 
			val.style.color = '#d5d5d5';
			countdown--; 
			setTimeout(function() { 
				settime(val) 
			},1000) 
		} 
	} 
	$('#sendCodeBtn').on('click',function(){
		var thisBtn = $(this);
		console.log($('#mobile').val())
		var url='sendsms?mobile='+$('#mobile').val()+'&cid='+tenant_id;
		$.get(url,function(data) {
			console.log(data);
			if(data && data.status){
				settime(thisBtn[0])
				$('#qrcode').focus()
			}else{
				alert(data.msg)
			}
		});
	})
	$('#loginBtn').on('click',function(){
		var mobile_ = $('#mobile').val();
		var code_ = $('#qrcode').val();
		var url = "login";
		$.post(url, {
			"mobile" : $('#mobile').val(),
			"code" : $('#qrcode').val(),
		}, function(data) {
			console.log(data);
			if(data && data.status){
				window.location.href = 'http://'+document.domain+'/'+path+'/open/me?srouce=login&cid='+tenant_id;
			}else{
				alert(data.msg)
			}
		});
	})
})
$('#mobile').keyup(function(){
		var val = $(this).val();
		if(val.length == 1 && val !='1'){
			$(this).val('')
		}
	})
</script>
</body>
</html>